--副本列表
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local FubenExcel = require("excel.copy").fuben
local Grid = require("bag.Grid")
local Broadcast = require("broadcast.Broadcast")
local Util = require("common.Util")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")

ID2COPYINFO = ID2COPYINFO or {} -- 对应副本id配置

COPY_STATE_NORMAL = 0 	-- 不显示倒计时
COPY_STATE_OPEN = 1 	-- 进行中，显示结束倒计时 
COPY_STATE_READY = 2 	-- 准备中，显示开启倒计时

function isOpen(CopyInfo, human)
	if not CopyInfo then 
		return false
	end
	local systemID = CopyInfo.config.systemID
	if not RoleSystemLogic.isOpen(human, systemID) then
		return false, RoleSystemLogic.getOpenDesc(systemID)
	end	
	return true
end

local function isRed(CopyInfo,human)
	if not isOpen(CopyInfo,human) then return end
	if not CopyInfo then return end
	if not CopyInfo.module then return end
	if not CopyInfo.module.isDot then return end
	return CopyInfo.module.isDot(human, CopyInfo)
end

local function getStartLeftTime(CopyInfo, human)
	if not CopyInfo then return end
	if not CopyInfo.module then return end 
	if not CopyInfo.module.getStartLeftTime then return end
	return CopyInfo.module.getStartLeftTime(human) 
end

local function getCopyState(CopyInfo, human)
	if not CopyInfo then return end
	if not CopyInfo.module then return end
	if not CopyInfo.module.getCopyState then return end
	return CopyInfo.module.getCopyState(human) 	
end

local function makeCopyList(human,copyID,CopyInfo,net)
	local config = CopyInfo.config
	net.id = copyID
	net.panelID = config.panelID
	net.bg = config.bg
	net.desc = config.desc
	net.lv = RoleSystemLogic.getOpenLv(config.systemID)
	net.showReward[0] = #config.showReward
	for i = 1, net.showReward[0] do
		local itemID = config.showReward[i][1]
		local itemCnt = config.showReward[i][2]
		Grid.makeItem(net.showReward[i], itemID, itemCnt)
	end

	net.isRed = isRed(CopyInfo, human) and 1 or 0
	net.startLefTime = getStartLeftTime(CopyInfo, human) or 0
	net.state = getCopyState(CopyInfo, human) or COPY_STATE_NORMAL
end

function CG_COPY_LIST_QUERY(human)
	local msgRet = Msg.gc.GC_COPY_LIST_QUERY
	msgRet.list[0] = 0
	local maxLen = #msgRet.list
	for copyID, CopyInfo in pairs(ID2COPYINFO) do
		if msgRet.list[0] >= maxLen then
			break
		end

		msgRet.list[0] = msgRet.list[0] + 1
		makeCopyList(human,copyID,CopyInfo,msgRet.list[msgRet.list[0]])
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet,human.fd)
end

function initCopyInfo(copyID, config)
	local moduleFn = nil 
	if config.moduleFn ~= "" then
		moduleFn = load("return require(\""..config.moduleFn.."\")")()		
	end

	local CopyInfo = {}
	CopyInfo.copyID = copyID
	CopyInfo.module = moduleFn
	CopyInfo.config = config
	ID2COPYINFO[copyID] = CopyInfo

	if moduleFn then
		moduleFn.CopyInfo = CopyInfo 	-- 子模块默认变量
	end
	return CopyInfo
end

-- 初始化
function init()	
	ID2COPYINFO = {}
	for copyID, config in pairs(FubenExcel) do 
		initCopyInfo(copyID, config)
	end	
end

function isDot(human,config)
	for k, CopyInfo in pairs(ID2COPYINFO) do
        if config.id == RoleSystemDefine.ROLE_SYS_ID_1201 or config.id == RoleSystemDefine.ROLE_SYS_ID_1202 then
            if (config.id == RoleSystemDefine.ROLE_SYS_ID_1201 and CopyInfo.config.tier == 1) or
               (config.id == RoleSystemDefine.ROLE_SYS_ID_1202 and CopyInfo.config.tier == 2) then
                if isRed(CopyInfo, human) then
		        	return true
		        end
            end
        else
		    if isRed(CopyInfo, human) then
		    	return true
		    end
        end
	end
end

ROLE_SYS_ID_1201 = 1201 -- 冒险界面-下层
ROLE_SYS_ID_1202 = 1202 -- 冒险界面-上层